package com.wiegand.util {
	import com.five3D.display.Bitmap3D;
	import com.five3D.display.Sprite3D;

	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;

	/**
	 * @author gandy
	 */
	public class Five3dCube extends Sprite3D {
		private var _front : Sprite;
		private var _back : Sprite;
		private var _top : Sprite;
		private var _bottom : Sprite;
		private var _left : Sprite;
		private var _right : Sprite;
		private var _materials : MaterialsList;
		private var _progress:Number = 0;
		private var _bitmapdatas : Array;

		public function  Five3dCube( materials:MaterialsList, cwidth:int=100, cdepth:int=100, cheight:int=100){
			
			_materials = materials;
			
			_front = new Sprite3D();
			_back = new Sprite3D();
			_top = new Sprite3D();
			_bottom = new Sprite3D();
			_left = new Sprite3D();
			_right = new Sprite3D();

			addChild(_front);
			addChild(_back);
			addChild(_bottom);
			addChild(_top);
			addChild(_left);
			addChild(_right);
			
			_bitmapdatas = new Array();
			
			var bitmapdata:BitmapData = new BitmapData(cwidth, cheight);
			bitmapdata.draw(Sprite( materials.getMaterialByName("front")));
			var texture:Bitmap3D = new Bitmap3D(bitmapdata, true, 2, 2);
			_front.addChild(texture);
			_bitmapdatas.push(bitmapdata);
			
			var bitmapdata:BitmapData = new BitmapData(cwidth, cheight);
			bitmapdata.draw(Sprite( materials.getMaterialByName("back")));
			texture = new Bitmap3D(bitmapdata, true, 2, 2);
			_back.addChild(texture);
			_bitmapdatas.push(bitmapdata);
			
			var bitmapdata:BitmapData = new BitmapData(cwidth, cheight);
			bitmapdata.draw(Sprite( materials.getMaterialByName("top")));
			texture = new Bitmap3D(bitmapdata, true, 2, 2);
			_top.addChild(texture);
			_bitmapdatas.push(bitmapdata);
			
			var bitmapdata:BitmapData = new BitmapData(cwidth, cheight);
			bitmapdata.draw(Sprite( materials.getMaterialByName("bottom")));
			texture = new Bitmap3D(bitmapdata, true, 2, 2);
			_bottom.addChild(texture);
			_bitmapdatas.push(bitmapdata);
			
			var bitmapdata:BitmapData = new BitmapData(cwidth, cheight);
			bitmapdata.draw(Sprite( materials.getMaterialByName("left")));
			texture = new Bitmap3D(bitmapdata, true, 2, 2);
			_left.addChild(texture);
			_bitmapdatas.push(bitmapdata);
			
			var bitmapdata:BitmapData = new BitmapData(cwidth, cheight);
			bitmapdata.draw(Sprite( materials.getMaterialByName("right")));
			texture = new Bitmap3D(bitmapdata, true, 2, 2);
			_right.addChild(texture);
			_bitmapdatas.push(bitmapdata);
						
			_front.x = - Math.round(cwidth/2);
			_front.y = - Math.round(cheight/2);
			_front.z = + Math.round(cdepth/2);

			_back.x = - Math.round(cwidth/2);
			_back.y = - Math.round(cheight/2);
			_back.z = - Math.round(cdepth/2);

			_left.x = - Math.round(cwidth/2);
			_left.y = - Math.round(cheight/2);
			_left.z = + Math.round(cdepth/2);
			_left.rotationY = 90;

			_right.x = + Math.round(cwidth/2);
			_right.y = - Math.round(cheight/2);
			_right.z = - Math.round(cdepth/2);
			_right.rotationY = -90;

			_top.x = - Math.round(cwidth/2);
			_top.y = + Math.round(cheight/2);
			_top.z = + Math.round(cdepth/2);
			_top.rotationX = -90;

			_bottom.x = - Math.round(cwidth/2);
			_bottom.y = - Math.round(cheight/2);
			_bottom.z = - Math.round(cdepth/2);
			_bottom.rotationX = 90;
			
			addEventListener(Event.ENTER_FRAME, cubeEnterFrameHandler);
			
		}
		
		public function getMaterialsList():MaterialsList{
			return _materials;
		}
		
		private function cubeEnterFrameHandler(event:Event):void { // HANDLER
			setTexturesBitmapData();
			_progress += .005;
			if (_progress > 1) _progress -= 1;
		}
		
		
		private function setTexturesBitmapData():void {
			var bitmapdata:BitmapData = getProgressBitmapDataMixed();
			_bitmapdatas[0].draw(bitmapdata, new Matrix(1, 0, 0, 1, -300, -300));
			_bitmapdatas[1].draw(bitmapdata, new Matrix(1, 0, 0, 1, -600, -300));
			_bitmapdatas[2].draw(bitmapdata, new Matrix(1, 0, 0, 1, -600, 0));
			_bitmapdatas[3].draw(bitmapdata, new Matrix(1, 0, 0, 1, 0, -300));
			_bitmapdatas[4].draw(bitmapdata, new Matrix(1, 0, 0, 1, -300, 0));
			_bitmapdatas[5].draw(bitmapdata);
		}
		
		private function getProgressBitmapDataMixed():BitmapData {
			var bitmapdatamixed:BitmapData;
			var bitmapdataspair:Object = getProgressBitmapDatasPair();
			bitmapdatamixed = bitmapdataspair.base.clone();
			bitmapdatamixed.draw(bitmapdataspair.addition, null, new ColorTransform(1, 1, 1, _progress%.25*4));
			return bitmapdatamixed;
		}
		
		private function getProgressBitmapDatasPair():Object {
			if (_progress < .25) return {base:_front, addition:_bottom};
			else if (_progress < .5) return {base:_bottom, addition:_back};
			else if (_progress < .75) return {base:_back, addition:_top};
			else return {base:_top, addition:_front};
		}
		
	}
}
